import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Article } from './article.interface';
import { Model } from 'mongoose';
import { CreateArticleDTO } from './article.dto';

@Injectable()
export class ArticleService {
  constructor(
    @InjectModel('Article') private readonly articleModule: Model<Article>,
  ) {}

  async findOne(_id: number): Promise<Article> {
    return await this.articleModule.findById(_id);
  }

  async finAll(): Promise<Article[]> {
    const articleLisr = await this.articleModule.find();
    return articleLisr;
  }

  async findPage(pageNum: number, pageSize: number): Promise<Article[]> {
    const articlePageList = await this.articleModule
      .find()
      .skip((pageNum - 1) * pageSize)
      .limit(pageSize);
    return articlePageList;
  }

  async addOne(body: CreateArticleDTO): Promise<any> {
    return this.articleModule.create(body);
  }
}
